package net.esj.basic.plugins.view.easyui;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.lang.RandomStringUtils;

import net.esj.basic.exception.MelonRuntimeException;
import net.esj.basic.plugins.view.easyui.utils.EasyUiFormUtil;
import net.esj.basic.utils.Validators;
import net.esj.basic.view.PageAction;


/**
 * 对于entity的setter&getter方法必须在子类中实现,且不能继承或重载
 * @author Administrator
 * 
 * @param <T>
 *            must be extends BasePojo
 */
public abstract class EasyUiAction<P,V> extends PageAction<P,V> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2017984924475912475L;
	
	private String action_id = RandomStringUtils.random(6, net.esj.basic.utils.StringUtils.getN36Chars());

	/**
	 * 加载数据到页面
	 * @throws MelonRuntimeException
	 */
	public void load()throws MelonRuntimeException {
		if (Validators.isEmpty(id)) {
			return;
		}
		doLoad();
		afterLoad();
		writeEasyUIFormJson(entity);
	}
	
	protected void writeEasyUIFormJson(Object o){
		PrintWriter out =null;
		getResponse().setContentType("text/text;charset=UTF-8");
		try {
			String result = EasyUiFormUtil.toEasyUiFormString(o, "entity");
			out = getResponse().getWriter();
			out.print(result);
			System.out.println(result);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (out != null) {
				out.close();
			}
		}
	}

	@Override
	protected boolean isAjax() {
		return true;
	}

	
}
